package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"shuqi-novel/config"
)

var DB *gorm.DB

var err error

func Mysqls() {

	mysqlq := config.AppConfig.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?",
		mysqlq.User,
		mysqlq.Password,
		mysqlq.Host,
		mysqlq.Port,
		mysqlq.Database,
	)
	DB, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		fmt.Println("数据库连接失败")
		return
	} else {
		fmt.Println("数据库连接成功")
	}

	err = DB.AutoMigrate(&User{}, &Book{})
	if err != nil {
		fmt.Println("迁移失败")
		return
	} else {
		fmt.Println("迁移成功")
	}

}
